package net.huashitong.index.ctrl;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;

import net.huashitong.index.dataModel.IndexTargetDataModel;
import net.huashitong.index.entity.DepartmentEntity;
import net.huashitong.index.entity.EnterpriseEntity;
import net.huashitong.index.entity.IndexInfoEntity;
import net.huashitong.index.entity.IndexTargetEntity;
import net.huashitong.index.filter.DepartmentFilter;
import net.huashitong.index.filter.EnterpriseFilter;
import net.huashitong.index.filter.IndexTargetFilter;
import net.huashitong.index.service.DepartmentService;
import net.huashitong.index.service.EnterpriseService;
import net.huashitong.index.service.IndexInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.naming.Name;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/web/index")
public class IndexInfoCtrl extends JsonBaseController {

    @Autowired
    private IndexInfoService indexInfoService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private EnterpriseService enterpriseService;

    @Override
    protected String getView(String s) {
        return "/modules/sys/eco/index/" + s;
    }

    //载入初始数据
    @RequestMapping(method = RequestMethod.GET)
    public String initPage(HttpServletRequest request, Model model) {
        return getView("query");
    }

    @ResponseBody
    @RequiresPermissions("web:index:query")
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute IndexTargetDataModel filter) {
        filter.setSource("cz");
        List<IndexTargetDataModel> query = indexInfoService.queryByFilter(filter);
        return coverJqGrid(filter, query);
    }


    /**
     * 导入指标
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public Message importIndex(@RequestParam(value = "file") MultipartFile file) {
        //导入指标
        if (file == null) {
            return coverMessage("500", "文件为空");
        }
        indexInfoService.saveImportIndex(file,"cz");//importIndex(file);
        return coverMessage("200");
    }

    /**
     * 添加指标
     * @param filter
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/addIndexCz", method = RequestMethod.GET)
    public String addIndexCz(@ModelAttribute IndexTargetFilter filter, Model model, HttpServletRequest request) {
        //县直单位
        DepartmentFilter departmentFilter1 = new DepartmentFilter();
        departmentFilter1.setDeptType("县直单位");
        List<DepartmentEntity> countyUnitList = departmentService.queryByFilterNoPage(departmentFilter1);
        //乡镇
        DepartmentFilter departmentFilter2 = new DepartmentFilter();
        departmentFilter2.setDeptType("乡镇");
        List<DepartmentEntity> townUnitList = departmentService.queryByFilterNoPage(departmentFilter2);
        EnterpriseFilter enterpriseFilter = new EnterpriseFilter();
        //企业
        List<EnterpriseEntity> enterpriseEntities = enterpriseService.queryByFilterNoPage(enterpriseFilter);
        //指标
        List<IndexInfoEntity> indexInfoEntityList = indexInfoService.query();
        model.addAttribute("countyUnitList", countyUnitList);
        model.addAttribute("townUnitList", townUnitList);
        model.addAttribute("enterpriseEntities", enterpriseEntities);
        model.addAttribute("indexInfoEntityList", indexInfoEntityList);
        model.addAttribute("target", filter);
        return getView("addIndex");
    }

    /**
     * 添加指标
     * @param filter
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addIndexCz", method = RequestMethod.POST)
    public Message addIndexCz(@ModelAttribute IndexTargetDataModel filter, HttpServletRequest request) {
        //查询指标
//        IndexInfoEntity indexInfo = indexInfoService.getById(filter.getIndexId());
        IndexInfoEntity indexInfo = new IndexInfoEntity();
        BeanUtils.copyProperties(filter,indexInfo);
        indexInfo.setSource("cz");
        indexInfoService.save(indexInfo);
        return coverMessage("200");
    }

    /**
     * 删除财政指标
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delIndexCz/{id}", method = RequestMethod.POST)
    public Message delIndexCz(@PathVariable List<String> id) {
        indexInfoService.batchDelete(id);
        return coverMessage("200");
    }

    /**
     * 修改财政指标
     * @param model
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/editIndexCz/{id}", method = RequestMethod.GET)
    public String showEdit(Model model, HttpServletRequest request, @PathVariable String id) {
        IndexInfoEntity indexInfo = indexInfoService.getById(id);
        model.addAttribute("indexInfo", indexInfo);
        return getView("form");
    }

    /**
     * 修改目标指标
     * @param filter
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editIndexCz", method = RequestMethod.POST)
    public Message editIndexCz(@ModelAttribute IndexTargetFilter filter, HttpServletRequest request) {
        IndexInfoEntity indexInfo = indexInfoService.getById(filter.getSysId());

        return coverMessage("200");
    }
}
